$(document).ready(function() {
	var canvas = $('#myCanvas');
	var ctx = canvas.get(0).getContext('2d');
	
	var canvasWidth = canvas.width();
	var canvasHeight = canvas.height();
	
	//桌布边界
	var billardWidth = 866;
	var billardHeiht = 432;
	
	//左上角
	var leftOffset = (canvasWidth - billardWidth)/2;
	var topOffset = (canvasHeight - billardHeiht)/2; 
	
	//台球对象
	var userBillard ;	//白球
	var billards ;	//其他球。数组
	
	//游戏设置
	var playGame; //播放控制
	var frameRate = 100;	//刷新帧率
	
	//初始化游戏
	function init(){
		ctx.fillStyle = "rgba(255,255,255,0.4)";
		
		//ctx.translate(leftOffset,billardHeiht);
		
		ctx.fillRect(leftOffset,topOffset,billardWidth,billardHeiht);
		
		
		//加载背景
//		var backGroundImg = new Image();
//		backGroundImg.src = "img/game2.png";
//		
//		$(backGroundImg).load(function() {
//			ctx.drawImage(backGroundImg, 0, 0, canvasWidth, canvasHeight);
			
//			var imgData = ctx.getImageData(0, 200, canvasWidth, 1);
//			
//			for(var i=0; i<315; i++){
//				ctx.putImageData(imgData,0,200+i);
//			}
//			
			
//			var pixels = imgData.data;
//			var height = 200;
//			var stopH = 300;
//			
//			for(var i=0; i<pixels.length; i+=4){
//				if(i > canvasWidth*height*4 && i < canvasWidth*(height+1)*4){
//					var val = i + canvasWidth*4*200;
//					pixels[val] = pixels[i];
//					pixels[val+1] = pixels[i+1];
//					pixels[val+2] = pixels[i+2];
//				}
//			}
//			
//			ctx.putImageData(imgData,0,0);
			
			//863*428
			//65*166
//			var grd = ctx.createLinearGradient(100,0, 843,0);
//			grd.addColorStop(0, "#0E1F2E");
//			grd.addColorStop(0.5, "#4FA0D8");
//			grd.addColorStop(1,"#0E1F2E");
//			ctx.fillStyle = grd;
//			ctx.fillRect(50,155, 863,428);
			
//			ctx.fillStyle = "#FFFFFF";

//			ctx.fillRect(50,155, 863,428);
//			ctx.fill();
			//#0E1F2E  #4FA0D8
//		});
		start();
	}
	
	//重置和启动游戏
	function start(){
		playGame = true;
		billards = new Array();
		
		//初始化白球
		var uBillardX = billardWidth/4 + leftOffset;
		var uBillardY = billardHeiht/2 + topOffset;
		var uBillardvX = 0;
		var uBillardvY = 0;
		var uBillardAX = 0;
		var uBillardAY = 0;
		var uBillardRadius = 13;
		var uBillardMass = 5;
		var uBillardFriction = 0.97;
		var uBillardPlayer = true;
		
		userBillard = new billard(uBillardX, uBillardY, uBillardvX, uBillardvY, 
			uBillardAX, uBillardAY, uBillardRadius, uBillardMass, uBillardFriction, uBillardPlayer);
		
		billards.push(userBillard);
		
		var firstX = billardWidth/4*3;
		var firstY = billardHeiht/2;
		var radius = 13;
		var mass = 5;
		
		for(var i=1; i<6; i++){
			for(var j=1; j<=i; j++){
				var x = Math.round(firstX + radius / Math.tan(Math.PI/6) * (i-1));
				var y = firstY + (i-1) * radius -(j-1) * 2 * radius;
				billards.push(new billard(x + leftOffset, y + topOffset, 0, 0, 0, 0, radius, mass, 0.97, false));
			}
		}
		
		for(var i=0; i< billards.length; i++){
			if(i==0){
				ctx.fillStyle = "#CFD0D0";
			}else {
				ctx.fillStyle = "#DC6E03";
			}
			var tmpBill = billards[i];
			ctx.beginPath();
			ctx.arc(tmpBill.x, tmpBill.y, tmpBill.radius, 0, Math.PI*2, true);
			ctx.closePath();
			ctx.fill();
		}
		
		$(window).mousedown(function(e) {
			canvasX = e.pageX ;
			canvasY = e.pageY - 99;
			if(canvasY > (billardHeiht + topOffset) && canvasX > 103 && canvasX < 430){
				isForce = true;
			}else {
				isAngle = true;
				isDrowAngle = true;
			}
		});
		
		$(window).mousemove(function(e) {
			if(isAngle || isForce){
				canvasX = e.pageX ;
				canvasY = e.pageY - 99;
			}
		});
		
		$(window).mouseup(function(e) {
			if(isForce){
				isForce = false;
				//计算加速度
				var force = 518 - canvasX;
				
				var dX = angleX - billards[0].x ;
				var dY = angleY - billards[0].y ;
				
				var angle = Math.atan2(dY,dX);
				
				billards[0].aX = force* Math.cos(angle) / mass;
				billards[0].aY = force* Math.sin(angle) / mass;
				
				isDrowAngle = false;
			} else if(isAngle){
				isAngle = false;
				angleX = canvasX;
				angleY = canvasY;
			}
		});
		
		//开始动画循环
		animate();
	}
	var isForce = false;
	var isAngle = false;
	var isDrowAngle = false;
	var angleX = -1;
	var angleY = -1;
	var canvasX = -1;
	var canvasY = -1;
	
	//动画
	function animate(){
		if(playGame){
			//清除
			ctx.clearRect(0, 0, canvasWidth, canvasHeight);
			
			if(canvasX>0){
				if(isDrowAngle){
					ctx.strokeStyle = "rgb(255, 255, 255)";
					ctx.lineWidth = 3;
					ctx.beginPath();
					ctx.moveTo(billards[0].x, billards[0].y);
					if(isAngle){
						ctx.lineTo(canvasX, canvasY);
					}else {
						ctx.lineTo(angleX, angleY);
					}
					ctx.closePath();
					ctx.stroke();
				}
					
				if(isForce){
					ctx.strokeStyle = "rgb(255,255,255)";
					ctx.lineWidth = 20;
					ctx.beginPath();
					ctx.moveTo(431,518);
					ctx.lineTo(canvasX,518);
					ctx.closePath();
					ctx.stroke();
				}
			}
			
			
			for(var i=0; i<billards.length; i++){
				var tmpBill = billards[i];
				
				if(tmpBill.aX != 0 || tmpBill.aY != 0){
					tmpBill.vX += tmpBill.aX;
					tmpBill.vY += tmpBill.aY;
					tmpBill.aX = 0;
					tmpBill.aY = 0;
				}
				
				//计算速度、位置
				if(Math.abs(tmpBill.vX) > 0.1 || Math.abs(tmpBill.vY) > 0.1){
					tmpBill.vX *= tmpBill.friction;
					tmpBill.vY *= tmpBill.friction;
					
					tmpBill.x += tmpBill.vX ;
					tmpBill.y += tmpBill.vY ;
				} else {
					tmpBill.vX = 0;
					tmpBill.vY = 0;
				}

				//碰撞检测、修正位置
				for(var j= i+1; j<billards.length; j++){
					var tmpBillB = billards[j];
					
					var dX = tmpBillB.x - tmpBill.x;
					var dY = tmpBillB.y - tmpBill.y;
					var distance = Math.sqrt((dX*dX)+(dY*dY));
					
					distance = Math.round(distance);
					
					if(distance < 2*tmpBill.radius){
						var angle = Math.atan2(dY,dX);
						
						var sine = Math.sin(angle);
						var cosine = Math.cos(angle);
						
						var x = 0;
						var y = 0;
						
						var xB = dX * cosine + dY * sine;
						var yB = dY * cosine - dX * sine;
						
						var vX = tmpBill.vX * cosine + tmpBill.vY * sine;
						var vY = tmpBill.vY * cosine - tmpBill.vX * sine;
						
						var vXB = tmpBillB.vX * cosine + tmpBillB.vY * sine;
						var vYB = tmpBillB.vY * cosine - tmpBillB.vX * sine;
						
//						vX *= -1;
//						vXB *= -1;
						var vTotal = vX - vXB;
						vX = vXB;
						vXB =vTotal + vX;
						
						//xB = x + 2* tmpBill.radius;
						
						tmpBill.x = tmpBill.x + (x * cosine - y * sine);
						tmpBill.y = tmpBill.y + (y * cosine + x * sine);
						
						tmpBillB.x = tmpBillB.x + (xB * cosine - yB * sine);
						tmpBillB.y = tmpBillB.y + (yB * cosine + xB * sine);
						
						tmpBill.vX = vX * cosine - vY * sine;
						tmpBill.vY = vY * cosine + vX * sine;
						
						tmpBillB.vX = vXB * cosine - vYB * sine;
						tmpBillB.vY = vYB * cosine + vXB * sine;
					}
				}

				//边界检测、修正位置
				if(tmpBill.x > billardWidth +leftOffset -tmpBill.radius){
					tmpBill.vX *= -1;
					tmpBill.x = billardWidth +leftOffset - tmpBill.radius;
				} else if(tmpBill.x < leftOffset + tmpBill.radius){
					tmpBill.vX *= -1;
					tmpBill.x = leftOffset + tmpBill.radius;
				}
					
				if(tmpBill.y > billardHeiht + topOffset - tmpBill.radius){
					tmpBill.vY *= -1;
					tmpBill.y = billardHeiht + topOffset -tmpBill.radius ;
				} else if(tmpBill.y < topOffset + tmpBill.radius){
					tmpBill.vY *= -1;
					tmpBill.y = topOffset + tmpBill.radius;
				}
			
				if(tmpBill.player){
					ctx.fillStyle = "#CFD0D0";
				}else {
					ctx.fillStyle = "#DC6E03";
				}
				
				ctx.beginPath();
				ctx.arc(tmpBill.x, tmpBill.y, tmpBill.radius, 0, Math.PI*2, true);
				ctx.closePath();
				ctx.fill();
				
			}
			
			setTimeout(animate, frameRate);
		}
	}
	
	init();
})

/**
 * 桌球对象
 * @param {number} x 横坐标
 * @param {number} y 纵坐标
 * @param {number} vX X轴移动速率
 * @param {number} vY Y轴移动速率
 * @param {number} aX 加速度
 * @param {number} aY
 * @param {number} radius 半径
 * @param {number} mass 质量
 * @param {number} friction 摩擦力
 * @param {boolean} player 玩家使用的球(白球)
 */
var billard = function(x, y, vX, vY, aX, aY, radius, mass, friction, player) {
	this.x = x;
	this.y = y;
	this.vX = vX;
	this.vY = vY;
	this.aX = aX;
	this.aY = aY;
	this.radius = radius;
	this.mass = mass;
	this.friction = friction;
	this.player = player;
}

